package bll

import (
	"context"
	"maya-service/store/mysql"
	"maya-service/utils"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type goodsCollector struct {
	iGoodsCollector store.IGoodsCollector
}

var GoodsCollector = &goodsCollector{
	iGoodsCollector: mysql.GoodsCollector,
}

func init() {
	Register(GoodsCollector)
}

func (a *goodsCollector) init() func() {
	return func() {}
}

// Create
func (a *goodsCollector) Create(ctx context.Context, in *model.GoodsCollectorCreateRequest) (int64, error) {
	var (
		err error
		id  int64
	)
	c := buildGoodsCollector(in)
	id, err = a.iGoodsCollector.Create(ctx, c)
	return id, err
}

// Update
func (a *goodsCollector) Update(ctx context.Context, in *model.GoodsCollectorUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.ThirdGoodsId != nil {
		dict["third_goods_id"] = in.ThirdGoodsId
	}

	if in.ImageURLList != nil {
		dict["image_url_list"] = utils.ToJsonString(in.ImageURLList)
	}

	if in.GoodsDetail != nil {
		dict["goods_detail"] = in.GoodsDetail
	}

	if in.GoodsPrice != nil {
		dict["goods_price"] = in.GoodsPrice
	}

	if in.GoodsStock != nil {
		dict["goods_stock"] = in.GoodsStock
	}

	if in.GoodsState != nil {
		dict["goods_state"] = in.GoodsState
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iGoodsCollector.Update(ctx, in.Id, dict)
}

// Delete
func (a *goodsCollector) Delete(ctx context.Context, in *model.GoodsCollectorDeleteRequest) error {
	return a.iGoodsCollector.Delete(ctx, in.Id)
}

// List
func (a *goodsCollector) List(ctx context.Context, in *model.GoodsCollectorListRequest) (*model.GoodsCollectorListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.GoodsCollector
		out   = &model.GoodsCollectorListResponse{}
	)

	if total, list, err = a.iGoodsCollector.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.GoodsCollectorsEntityToDto(list)

	return out, nil
}

// Find
func (a *goodsCollector) Find(ctx context.Context, in *model.GoodsCollectorInfoRequest) (*model.GoodsCollectorInfo, error) {
	var (
		err  error
		data *entity.GoodsCollector
		out  = &model.GoodsCollectorInfo{}
	)

	if data, err = a.iGoodsCollector.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.GoodsCollectorEntityToDto(data)
	return out, nil
}

// buildGoodsCollector build entity
func buildGoodsCollector(in *model.GoodsCollectorCreateRequest) *entity.GoodsCollector {

	ety := &entity.GoodsCollector{}

	ety.ThirdGoodsId = in.ThirdGoodsId

	ety.ImageURLList = utils.ToJsonString(in.ImageURLList)

	ety.GoodsDetail = in.GoodsDetail

	ety.GoodsStock = in.GoodsStock

	ety.GoodsState = in.GoodsState

	ety.GoodsName = in.GoodsName

	ety.GoodsPrice = in.GoodsPrice

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
